package ua.kyivenergo.cableLines.data;

import ua.kyivenergo.cableLines.data.attributes.Attribute;
import ua.kyivenergo.cableLines.messages.Messages;
import ua.kyivenergo.cableLines.search.SearchComparator;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Alex Kebkalo (kebka@mail.ru) on 18.04.14.
 */
public class GenericObject {
    String objectType;
    List<Attribute> attributes;

    public GenericObject(String objectType) {
        this.objectType = objectType;
        attributes = new ArrayList<>();
    }

    public void addAttribute(Attribute attribute) {
        attributes.add(attribute);
    }

    public String getType() {
        return objectType;
    }

    public String getAttributeText(String description) {
        final String NEXT_LINE = "<br/>";
        String text = "";
        for (Attribute attribute : attributes) {
            if (attribute.getDescription().equals(description)) {
                text = String.format("%s%s %s %s%s", text, Messages.getPrefix(objectType), attribute.toString(), Messages.getSuffix(objectType), NEXT_LINE);
            }
        }
        if (text.length() >= NEXT_LINE.length()) {
            text = text.substring(0, text.length() - NEXT_LINE.length());
        }
        return text;
    }

    public double getRelevance(String searchString) {
        double relevance = SearchComparator.compareStrings(Messages.getPrefix(objectType), searchString);
        double currentCompare = SearchComparator.compareStrings(Messages.getSuffix(objectType), searchString);
        if (currentCompare > relevance) {
            relevance = currentCompare;
        }

        for (Attribute a : attributes) {
            currentCompare = a.getRelevance(searchString);
            if (currentCompare > relevance) {
                relevance = currentCompare;
            }
        }
        return relevance;
    }
}
